#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "/home/limenghui/ns3/ns-allinone-3.26/ns-3.26/src/aodv/doc/aodv.h"
#include "/home/limenghui/ns3/ns-allinone-3.26/ns-3.26/src/aodv/helper/aodv-helper.h"
#include "ns3/dsr-helper.h"
#include "ns3/dsr-main-helper.h"
using namespace ns3;


NS_LOG_COMPONENT_DEFINE ("aodv hoc net");
int
main (int argc, char *argv[])
{
  CommandLine cmd;
  cmd.Parse (argc, argv);

  Time::SetResolution (Time::NS);
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

  NodeContainer adHocNodes;
  adHocNodes.Create (30);

  WifiHelper wifi;//wu li ceng he  mac ceng
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
  wifiPhy.SetChannel (wifiChannel.Create ());
  NqosWifiMacHelper mac = NqosWifiMacHelper::Default ();
  mac.SetType ("ns3::AdhocWifiMac");
  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager");
  NetDeviceContainer adHocDevices = wifi.Install (wifiPhy, mac, adHocNodes);

  AodvHelper aodv;
  InternetStackHelper stack;
  stack.SetRoutingHelper (aodv);
  stack.Install (adHocNodes);


  Ipv4AddressHelper address;
  address.SetBase("195.1.1.0","255.255.255.0");
  Ipv4InterfaceContainer AdHocIp;
  AdHocIp = address.Assign(adHocDevices);

  MobilityHelper mobility;
  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
                                   "MinX", DoubleValue (0.0),
                                   "MinY", DoubleValue (0.0),
                                   "DeltaX", DoubleValue (50.0),
                                   "DeltaY", DoubleValue (50.0),
                                   "GridWidth", UintegerValue (14),
                                   "LayoutType", StringValue ("RowFirst"));

  mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
                               "Bounds", RectangleValue (Rectangle (-100, 100, -100, 100)));
  mobility.Install (adHocNodes);



  NS_LOG_INFO ("Create Applications.");
  uint16_t port = 9999;
  OnOffHelper onOff1("ns3::TcpSocketFactory",Address(InetSocketAddress(AdHocIp.GetAddress(0),port)));
  onOff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
  onOff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));

  ApplicationContainer apps1 = onOff1.Install(adHocNodes);
  apps1.Start(Seconds(1.0));
  apps1.Stop(Seconds(500.0));

  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", Address(InetSocketAddress (Ipv4Address::GetAny(), port)));
  ApplicationContainer apps2 = sinkHelper.Install(adHocNodes.Get(0));

  apps2.Start(Seconds(0.0));
  apps2.Stop(Seconds(500.0));

  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  Simulator::Stop(Seconds(500.0));
  Simulator::Run();
  Simulator::Destroy();

  return 0;
}
